//
//  WCNSendQuestionVC.m
//  WCN
//
//  Created by wsj2015 on 16/1/31.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNSendQuestionVC.h"
#import "Masonry.h"
#import "WCChooseItem.h"
#import "WCQestionnaireModel.h"
#import "WCPatientInforList.h"
#import "WCQuestionListCell.h"
#import "WCQuestionnaireDetailVC.h"
#import "WSJPickView.h"

#define patientId_key @"patientId"
#define patientName_key @"patientName"
@interface WCNSendQuestionVC ()<UIAlertViewDelegate>


@property (nonatomic,  strong) UIView *headView;

//

@property (nonatomic,  assign) NSInteger qbelong;

@property (nonatomic,  retain) NSMutableArray   *dataArray;
@property (nonatomic,  retain) NSMutableArray *patientContactArray;


@property (nonatomic,  assign) NSInteger pickedQuestionId;
//@property (nonatomic,  retain) WSJPickView *picker;



@end

@implementation WCNSendQuestionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    [self.tableView.header beginRefreshing];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.pickedQuestionId = -1;
        self.qbelong = 1;//默认进来 系统
    }
    return self;
}

- (NSMutableArray *)patientContactArray{
    if (!_patientContactArray) {
        _patientContactArray =[NSMutableArray array];
    }
    return _patientContactArray;
    
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;

}
//- (WSJPickView *)picker{
//    if (_picker ==nil ) {
//        _picker =[WSJPickView pickView];
//        defineWeakSelf;
//        _picker.pickTitle = @"护士选择";
//        _picker.saveBlock= ^(NSString *nurseName,NSInteger index){
//            WCPatientInforList *model= weakSelf.patientContactArray[index];
//            [weakSelf sendQuestionnarieToPatient:[model.recordId integerValue]];
//        };
//        
//    }
//    return _picker;
//    
//}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex== 0) {
//        [self postPatientContactRecordList];
       
    }
}
- (void)configUI{
    
    [self addRightBarButtonItemWithTitle:@"确认" target:self action:@selector(confirmSend)];
     self.title =@"发送问卷";
    weakSelf_define;
    WCChooseItem *chooseItem =[WCChooseItem viewFromNib];
//    chooseItem.frame  =CGRectMake(0, 0, kScreenWidth, 39);
    chooseItem.itemCallBack = ^(NSInteger currentItem){
        weakSelf.qbelong = currentItem==1?1:0;
        [self.tableView.header beginRefreshing];
        
    };
    [self.view addSubview:chooseItem];
    [self.tableView registerNib:[UINib nibWithNibName:@"WCQuestionListCell" bundle:nil] forCellReuseIdentifier:@"WCQuestionListCellId"];
  

    [chooseItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.mas_equalTo(40);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(chooseItem.mas_bottom);
        make.left.equalTo(self.view.mas_left);
         make.right.equalTo(self.view.mas_right);
         make.bottom.equalTo(self.view.mas_bottom);
    }];
  
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf postQestionnareList];
    }];
    
    

}

- (void)confirmSend{
    
    
    
    if (self.pickedQuestionId == -1) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"请选择一个问卷" withView:self.view ];
        return;
    }
    [self sendQuestionnarieToPatient];
    
//      [self postPatientContactRecordList];
//    UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"提示" message:@"是否确定发送" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重新选择", nil];
//    [alertView show];


}


//#pragma mark -请求随访患者列表
//- (void) postPatientContactRecordList{
//    
//    [WCPopupLayerManager showLodingView];
//    weakSelf_define;
//    NSInteger nursId =[[WCDefaultManager shareMg] nurseId];
//    [[WCNetworkAccessManager sharedMg] POST:kPatientContactRecordList parameters:@{@"nurseId":@(nursId)} success:^(NSDictionary * responseObject) {
//        [weakSelf.patientContactArray removeAllObjects];
//        for (NSDictionary  *dict in responseObject[@"data"]) {
//            [weakSelf.patientContactArray addObject:[WCPatientInforList objectWithKeyValues:dict]];
//            
//        }
//        NSMutableArray *titleArray =[NSMutableArray array];
//        for ( WCPatientInforList *model in weakSelf.patientContactArray) {
//            //防止错误数据导致崩溃
//            if (!model.name) {
//                [weakSelf.patientContactArray removeObject:model];
//            }else{
//                [titleArray addObject:model.name];
//            }
//            
//        }
//        weakSelf.picker.dataArray =titleArray;
//        
//        [weakSelf.picker show];
//       
//       
//        
//    } failure:nil netError:nil always:^{
//        [WCPopupLayerManager dissmissLodingView];
//       
//    }];
//    
//    
//}

#pragma mark -发送问卷
- (void)sendQuestionnarieToPatient{
    
    [WCPopupLayerManager showLodingView];
    weakSelf_define;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"questionId"]=@(self.pickedQuestionId);
    params[@"recordId"]=@(self.recordId);
    params[@"nurseId"]=@([[WCDefaultManager shareMg] nurseId]);
    params[@"recordFlag"] =@(1);
//    NSDictionary *params =@{@"questionId":@(self.pickedQuestionId),
//                            @"recordId":@(self.recordId),
//                            @"nurseId":@([[WCDefaultManager shareMg] nurseId])};
    [[WCNetworkAccessManager sharedMg] POST:kSendQuestionToPatient parameters:params success:^(NSDictionary *responseObject) {
        CoreSVPSuccess(responseObject[@"data"]);
        if (weakSelf.isAutoRefresh) {
             weakSelf.isAutoRefresh();
        }
       
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:nil netError:nil always:^{
        [WCPopupLayerManager dissmissLodingView];
    }];
    
    
    
}

#pragma mark -请qiu问卷列表数据
- (void)postQestionnareList{
    defineWeakSelf;
    [WCPopupLayerManager showLodingView];
    //    "qbelong": 0,
    //    "nurseId": 87
    
        NSMutableDictionary *param =[NSMutableDictionary dictionary];
        param[@"qbelong"] =@(self.qbelong);
        param[@"nurseId"] =@([[WCDefaultManager shareMg] nurseId]);
        param[@"qflag"] =@(1);
    
    [[WCNetworkAccessManager sharedMg] POST:kQuestionnaireSendList parameters:param success:^(NSDictionary *responseObject) {
//        self.pickedQuestionId = -1;
        [weakSelf.dataArray removeAllObjects];
        for (NSDictionary *dict in responseObject[@"data"]) {
            WCQestionnaireModel *mdel =[WCQestionnaireModel new];
            [mdel setValuesForKeysWithDictionary:dict];
            [weakSelf.dataArray addObject:mdel];
        }
        [weakSelf.tableView reloadData];
        
    } failure:nil netError:nil always:^{
        [WCPopupLayerManager dissmissLodingView];
        [weakSelf.tableView.header endRefreshing];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WCQuestionListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCQuestionListCellId" forIndexPath:indexPath];
    WCQestionnaireModel *model =self.dataArray[indexPath.row];
   
    model.isSelect =[model.questionId integerValue] ==self.pickedQuestionId?YES:NO;
    cell.cellTypeForSelect = YES;
    
    cell.model =model;
  
    
    
    return  cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WCQestionnaireModel *model =self.dataArray[indexPath.row];
    self.pickedQuestionId =[model.questionId integerValue];
    [self.tableView reloadData];
    
}

@end
